<template>
  <div class="detail-container">
    <el-form
      ref="postForm"
      :model="postForm"
      :rules="rules"
      label-width="100px"
      label-suffix=":"
    >

      <sticky :z-index="10" :class-name="'sub-navbar ' + postForm.color">
        <el-button v-loading="loading" type="success" @click="submitForm">
          {{ isEdit ? '更新' : '保存' }}
        </el-button>
      </sticky>

      <div class="app-container" style="padding: 10px 20px">
        <warn />

        <el-row>
          <el-col :span="22">
            <el-form-item label="推荐人">
              <el-select
                v-model="postForm.referrer_id"
                placeholder="推荐人"
                class="filter-item"
                filterable
              >
                <el-option
                  v-for="item in referrerList"
                  :key="item.id"
                  :label="`${item.id} (${item.mobile})`"
                  :value="+item.id"
                  style="color: red"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="手机号码" prop="mobile" required>
              <el-input v-model.trim="postForm.mobile" style="width: 200px" />
            </el-form-item>
            <el-form-item label="昵称">
              <el-input v-model.trim="postForm.nickname" style="width: 200px" />
            </el-form-item>
            <el-form-item label="等级">
              <el-select
                v-model="postForm.level"
                placeholder="等级"
                class="filter-item"
                filterable
              >
                <el-option
                  v-for="item in levelList"
                  :key="item.id"
                  :label="item.display"
                  :value="+item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="新等级" v-if="false">
              <el-select
                v-model="postForm.level2"
                placeholder="等级"
                class="filter-item"
                filterable
              >
                <el-option
                  v-for="item in newLevel"
                  :key="item.id * 100"
                  :label="item.name"
                  :value="+item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="状态" style="width: 200px">
              <el-switch v-model="postForm.status" />
            </el-form-item>
          </el-col>
        </el-row>
      </div>

    </el-form>
  </div>
</template>

<script>
import api from '@/api'
import Sticky from '@/components/Sticky'
import Warn from './Warn'
export default {
  name: 'MemberDetail',
  components: {
    Sticky,
    Warn
  },
  props: {
    /**
     * 是否为编辑
     */
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    const mobileValidate = (rule, value, callback) => {
      if (value === undefined || value.length <= 0) {
        callback(new Error('请输入手机号码'))
      } else if (!/^1[3456789]\d{9}$/.test(value)) {
        callback(new Error('手机号码错误'))
      } else {
        callback()
      }
    }
    return {
      id: -1,
      loading: false,
      postForm: {
        color: 'draft',
        status: true,
        referrer_id: 0,
        level: 0,
        level2: 0
      },
      rules: {
        mobile: [{ validator: mobileValidate, trigger: 'blur' }]
      },
      referrerList: [],
      levelList: [],
      // 新等级
      newLevel: []
    }
  },
  created() {
    /**
     * 获取编辑数据
     */
    if (this.isEdit) {
      this.id = (this.$route.params && this.$route.params.id) || -1
      this.fetchData(this.id)
    }
    this.getReferrerList()
    this.getLevelList()
    this.getNewLevelList()
  },
  methods: {
    async getReferrerList() {
      const response = await api.member.userReferrerList()
      this.referrerList = response.data
    },
    async getLevelList() {
      const response = await api.member.levelList()
      this.levelList = response.data
    },
    async getNewLevelList() {
      const response = await api.member.newLevel()
      this.newLevel = response.data
    },
    /**
     * 获取编辑数据
     */
    async fetchData(id) {
      const response = await api.member.view({ id })
      this.postForm = Object.assign({}, response.data)
      // 状态
      this.postForm.status = +this.postForm.status === 1
      // 推荐人, 等级
      this.postForm.referrer_id = +this.postForm.referrer_id
      this.postForm.level = +this.postForm.level
      // this.postForm.level2 = +this.postForm.new_level
      this.$set(this.postForm, 'level2', +this.postForm.new_level)
    },
    /**
     * 提交表单
     */
    submitForm() {
      this.$refs.postForm.validate(valid => {
        if (valid) {
          this.loading = true
          // 状态
          const status = this.postForm.status ? 1 : 0
          api.member.edit({
            id: this.id,
            ...this.postForm,
            status
          }).then(response => {
            this.loading = false
            this.$notify({
              title: '成功',
              message: response.message || '操作成功',
              type: 'success',
              duration: 2000
            })
            this.$router.push(`/member/edit/${response.data.id}`)
          }).finally(() => {
            this.loading = false
          })
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.detail-container {
  //
}
</style>
